

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import View.*;

/**
 * Servlet for administrative operations (Add/edit items, categories and users)
 */
@WebServlet("/user/admin/admin")
public class AdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	/**
	 * Auto-generated constructor
	 */
    public AdminServlet() {
        super();
    }

    /*
     * Called when a get request is sent from a page. Redirects to doPost() for privacy in the url.
     */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/*
	 * Controls everything from the administrative page, depending on what the hidden value is set to.
	 * Calls the controller and view classes when information is needed. No direct connection to the underlying layers.
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int form = Integer.parseInt(request.getParameter("hiddenField"));	// Which button was pressed to know where to direct the work.
		ItemView item = null;
		UserView user = null;
		switch (form) {
		case 0: // Fetch item by Id
			int itemId = Integer.parseInt(request.getParameter("itemTextField"));
			item = Controller.getItemById(itemId);
			request.setAttribute("item", item);
			break;
		case 1: // Add new item or update existing item depending on if the id textfield is filled in or not.
			String stringItemId = request.getParameter("id_text");
			int itemid = 0;
			if (!stringItemId.equals("")) {
				itemid = Integer.parseInt(stringItemId);
			}
			String name = (String) request.getParameter("name_text");
			int categoryId = Integer.parseInt(request.getParameter("item_dropdown"));
			int amount = Integer.parseInt(request.getParameter("amount_text"));
			String description = (String) request.getParameter("description_text");
			float price = Float.valueOf((String) request.getParameter("price_text"));
			if (itemid == 0) {
				Controller.createItem(categoryId, name, price, description, amount);
			} else {
				Controller.updateItem(itemid, categoryId, name, price, description, amount);
			}
			break;
		case 2: // Remove category by Id. Will only do it if a category does not contain submenu's or items.
			int removedCategoryId = Integer.parseInt(request.getParameter("removeCategoryDropdown"));
			if (Controller.removeCategory(removedCategoryId)) {
				System.out.println("Remove successful");
			}else {
				System.out.println("Remove unsuccessful");
			}
			break;
		case 3: // Add new category. Dependant on if the category dropdown has a value or not. If it does it'll create a sub category in the selected category.
				// Otherwise the category will be in root.
			int parentId = Integer.parseInt(request.getParameter("addCategoryDropdown"));
			String categoryName = (String) request.getParameter("subMenu_text");
			Controller.addCategory(categoryName, parentId);
			break;
		case 4: // Fetch user by username.
			String getUsername = (String) request.getParameter("getUsername_text");
			user = Controller.getUserByUsername(getUsername);
			request.setAttribute("userInfo", user);
			break;
		case 5: // Edit user fields. All besides password, since we don't want the password in the view.
			int userId = Integer.parseInt(request.getParameter("id_text"));
			String username = (String) request.getParameter("username_text");
			int rank = Integer.parseInt((String) request.getParameter("rank_text"));
			String address = (String) request.getParameter("address_text");
			String zipcode = (String) request.getParameter("zipcode_text");
			String city = (String) request.getParameter("city_text");
			String email = (String) request.getParameter("email_text");
			String firstname = (String) request.getParameter("firstname_text");
			String surname = (String) request.getParameter("surname_text");
			Controller.editUser(userId, username, rank, address, zipcode, city, email, firstname, surname);
			break;
		case 6: // Edit a category selected from the dropdown menu.
			int editCategoryId = Integer.parseInt(request.getParameter("editCategoryDropdown"));
			String newCategoryName = (String) request.getParameter("editCategory_text");
			Controller.editCategory(editCategoryId, newCategoryName);
			break;
		}
		// After the work above is done, redirect back to the administrative page for updated view.
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/user/admin/admin.jsp");
		dispatcher.forward(request, response);
	}

}
